﻿var modelid = DcGlobal.getQueryString("id");
$(document).ready(function() {
    var community = new Chenxin.UI.CommunityMgr({
        currentId: modelid,
        createGrid: Chenxin.emptyFn,
        createSearch: Chenxin.emptyFn,
        clearCachInfo: function() {
            var idList = this.currentId;
            if (!idList) {
                alert('请先选择楼盘记录！');
                return;
            }
            Chenxin.ajax({
                type: "get",
                dataType: "json",
                url: this.actionUrl,
                data: { op: 'clearbuildingcache', idList: idList },
                onSuccess: function(data, msg) {
                    alert(msg);
                }
            });
        },
        logicDelInfo: function(autoConfirm, ch) {//删除操作
            if (!this.currentId) {
                alert('请选择一条记录！');
                return;
            }
            var _this = this;
            var d = true;
            if (autoConfirm) {
                d = confirm('确定要删除该条记录吗？');
            }
            if (d) {
                Chenxin.ajax({
                    type: "get",
                    dataType: "json",
                    url: this.actionUrl,
                    data: { op: 'logicdel', modelid: this.currentId, checkhouse: ch || 'true' },
                    onSuccess: function(data, msg) {
                        if (data.hasHouse) {
                            if (confirm('本小区已经添加过房源，确定执行逻辑删除吗？')) {
                                _this.logicDelInfo(false, 'false');
                            }
                        } else {
                            alert(msg);
                            if (_this.logicDelSucc) {
                                _this.logicDelSucc();
                            }
                        }
                    }
                });
            }
        },
        logicDelBuildingName: function(autoConfirm, ch) {
            var _this = this;
            var idList = this.buildIngMgr.grid.getSelectList();
            if (!idList) {
                alert('请先选择楼栋记录！');
                return;
            }
            Chenxin.ajax({
                type: "get",
                dataType: "json",
                url: this.actionUrl,
                data: { op: 'logicDelBuildingName', idList: idList },
                onSuccess: function(data, msg) {
                    alert(msg);
                    _this.buildIngMgr.grid.reload('',0);
                }
            });
        },
        afterDelSucc: function() {
            window.location.href = '/CommunityMgr.aspx';
        },
        logicDelSucc: function() {
            //window.location.href = '/CommunityMgr.aspx';
			this.loadInfo();
        },
        createHouseBuilding: function() {
            if (!this.buildIngMgr) {

                var _obj = this;
                this.buildIngMgr = new Chenxin.UI.CommunityBuildingMgr({
                    createGrid: function() {
                        return new Chenxin.UI.Grid({
                            actionUrl: 'service/CommunityInfo.ashx?op=getlist&communityid=' + _obj.currentId,
                            id: 'communityinfo',
                            renderTo: 'info_table',
							title:'楼盘楼栋信息',
                            columns: {
                                BuildingName: {
                                    head: '楼栋名称',
                                    width: 100,
                                    sortable: true
                                },
                                Floor: {
                                    head: '层数',
                                    width: 50,
                                    sortable: true
                                },
                                UnitNum: {
                                    head: '单元数',
                                    width: 50,
                                    sortable: true
                                },
                                UnitType: {
                                    head: '单元类型',
                                    width: 70,
                                    sortable: true
                                },
                                RoomRuleRemark: {
                                    head: '楼栋规则',
                                    width: 200,
                                    sortable: true
                                },
                                AddPerson: {
                                    head: '创建人',
                                    width: 80,
                                    sortable: true
                                },
                                AddTime: {
                                    head: '	创建时间',
                                    width: 80,
                                    sortable: true
                                },
                                Status: {
                                    head: '状态',
                                    width: 60,
                                    sortable: true
                                }
                            },
                            scope: this,
                            afterSelect: function(id, el) {
                                var _this = this;
                                _this.currentId = id;
                            }
                        });
                    },
					createSearch:function(){
						return new Chenxin.UI.CommunityBuildingSearch({scope:this});
					},
					autoLoadGrid: false
                });
            }
            if (modelid) {
                this.buildIngMgr.communityId = modelid;
            }
        },
        initEvent: function() {
            var _this = this;
            //编辑楼盘
            $('#btn_edit').click(function() {
                _this.currentId = modelid;
                _this.edit();
            });
            //删除楼盘
            $('#btn_del').click(function() {
                _this.currentId = modelid;
                _this.del();
            });
            //逻辑删除楼盘
            $('#btn_logicdel').click(function() {
                _this.currentId = modelid;
                _this.logicDelInfo();
            })
            //批量逻辑删除楼栋
            $('#btn_batchlogicdel').click(function() {
                _this.logicDelBuildingName();
            })
            //添加楼栋
            $('#btn_add_building').click(function() {
                _this.currentId = modelid;
                _this.buildIngMgr.add();
            });
            //批量添加楼栋
            $('#btn_bulkadd_building').live('click', function() {
                _this.buildIngMgr.bulkAdd();
            });
            //清楚缓存
            $('#btn_clearcach').click(function() {
                _this.currentId = modelid;
                _this.clearCachInfo();
            });
			
			
			$('#btn_lock').click(function(){
				var _status=_this.model.Status;
				if(_status=="-10"){
					alert('状态为“冻结”的楼盘，不能执行冻结操作！');
					return false;
				}else if(_status=="-1"){			
					alert('状态为“已删除”的楼盘，不能执行冻结操作！');	
					return false;
				}
				_this.logicDel(true,'false','确定冻结该楼盘吗？该楼盘被冻结后将不能添加相同名称楼盘，且不能添加房源！','lock');
				return false;		
			});		
			$('#btn_unlock').click(function(){
				var _status=_this.model.Status;
				if(_status=="1"){				
					alert('状态为“正常”的楼盘，不能执行激活操作！');
					return false;
				}else if(_status=="-1"){				
					alert('状态为“已删除”的楼盘，不能执行激活操作！');
					return false;
				}
				_this.logicDel(true,'false','确定激活该楼盘吗？','unlock');
				return false;		
			});		

			
			$('#btn_lockbuilding').live('click',function(){
				if(!_this.buildIngMgr.currentId){
					alert('请选择一条记录！');
					return false;
				}
				var _status=_this.buildIngMgr.getStatus(9);
				if(_status=="已冻结"){
					alert('状态为“冻结”的楼栋，不能执行冻结操作！');
					return false;
				}else if(_status=="已删除"){			
					alert('状态为“已删除”的楼栋，不能执行冻结操作！');	
					return false;
				}
				_this.buildIngMgr.logicDel(true,'false','确定冻结该楼栋吗？该楼盘被冻结后将不能添加房源！','lock');
				return false;
			});
			
			$('#btn_unlockbuilding').live('click',function(){
				if(!_this.buildIngMgr.currentId){
					alert('请选择一条记录！');
					return false;
				}
				var _status=_this.buildIngMgr.getStatus(9);
				if(_status=="正常"){				
					alert('状态为“正常”的楼栋，不能执行激活操作！');
					return false;
				}else if(_status=="已删除"){				
					alert('状态为“已删除”的楼栋，不能执行激活操作！');
					return false;
				}
				_this.buildIngMgr.logicDel(true,'false','确定激活该楼栋吗？','unlock');
				
				return false;
			});
        },
        createForm: function() {
            return new Chenxin.UI.CommunityBaseInfoForm({
                scope: this,
                //autoAlertSucc:false,
                beforeFitData: function(model) {
                    var company = this.form.findByName('CompanyId');
                    var region = this.form.findByName('Region');
                    var block = this.form.findByName('Block');
                    var name = this.form.findByName('CommunityName');
                    var address = this.form.findByName('Address');
                    var city = this.form.findByName('CityId');
                    if (this.form.op == 'edit') {
                        city.setDisable();
                        company.setDisable();
                        // region.setDisable();
                        // block.setDisable();			
                        name.unFormValidator(true);
                        name.attr('disabled', 'disabled');
                        address.unFormValidator(true);
                        address.attr('disabled', 'disabled');

                        company.param.fatherid = model.CityId;
                        company.reload(model.CompanyId);

                        region.block = model.Block;
                        region.param.fatherid = model.CompanyId;
                        region.reload(model.Region);
                    } else {
                        city.setEnable();
                        company.setEnable();
                        // region.setEnable();
                        // block.setEnable();			
                        name.unFormValidator(false);
                        name.attr('disabled', '');
                        address.unFormValidator(false);
                        address.attr('disabled', '');

                        company.clearItems();
                        region.clearItems();
                        block.clearItems();
                    }
                },
                saveSucc: function(data, msg) {
                    this.form.hide();
                }
            });
        },
        customerInit: function() {
            this.createHouseBuilding();
            this.loadInfo();
        },
		loadInfo:function(){
			Chenxin.Fn.getModel(modelid, this.actionUrl, function(data, msg) {
				$('#PublicTable span').each(function() {
					if(this.id=='StatusDesc'){
						if(data.Status==1){
							$('#' + this.id).css('color','#009900');
							data.StatusDesc='正常';				
						}else if(data.Status==-1){
							$('#' + this.id).css('color','#ff0000');
							data.StatusDesc='已删除';
						}else if(data.Status==-10){
							$('#' + this.id).css('color','#0000ff');
							data.StatusDesc='已冻结';
						}
					}
				
					$('#' + this.id).text(data[this.id]);
				});
				this.model=data;
			}, this)
		}
    });
});

